package com.example.lots_of_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lots_of_demo.bo.PermissionResBo;
import com.example.lots_of_demo.dao.PermissionDao;
import com.example.lots_of_demo.dao.PermissionGroupDao;
import com.example.lots_of_demo.dao.RPermissionResDao;
import com.example.lots_of_demo.dao.RRolePermissionDao;
import com.example.lots_of_demo.model.*;
import com.example.lots_of_demo.po.PermissionGroupPo;
import com.example.lots_of_demo.po.PermissionGroupRoleIdPo;
import com.example.lots_of_demo.service.PermissionService;
import com.example.lots_of_demo.utils.PermissionGroupUtils;
import com.example.lots_of_demo.utils.R;
import com.example.lots_of_demo.vo.PermissionGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.function.BiFunction;

/**
 * @author : LZJ
 * @description : code for LZJ
 * @date : 2021/2/16
 */
@Service
public class PermissionServiceImpl  extends ServiceImpl<PermissionDao, Permission> implements PermissionService {

    @Autowired
    private PermissionGroupDao permissionGroupDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RPermissionResDao rPermissionResDao;
    @Autowired
    private RRolePermissionDao rRolePermissionDao;

    @Override
    public List<PermissionGroupVo> getPermissionGroup() {
        LambdaQueryWrapper<PermissionGroup> permissionGroupQueryWrapper = new LambdaQueryWrapper<>();
        permissionGroupQueryWrapper.eq(PermissionGroup::getEnable, PermissionGroupEnum.PermissionGroupEnable.IS_ENABLE.getCode());
        List<PermissionGroup> permissionGroups = permissionGroupDao.selectList(permissionGroupQueryWrapper);
        List<PermissionResBo> permissionResBos = permissionDao.getResGroupByPermissionCode();
        return PermissionGroupUtils.transferPermissionGroupVo(permissionGroups, permissionResBos);
    }

    @Override
    public List<Integer> getCheckedPermissionGroupByRoleId(Long id) {
        return permissionGroupDao.getCheckedPermissionGroupByRoleId(id);
    }

    @Override
    @Transactional
    public R endowPermission(PermissionGroupRoleIdPo permissionGroupRoleIdPo) {
        permissionGroupRoleIdPo.getIds().forEach(permissionGroupPo -> {
            if (!ObjectUtils.isEmpty(permissionGroupPo.getPermissionGroupId())) {
                PermissionGroup permissionGroup = permissionGroupDao.selectPermissionGroup(PermissionGroupUtils.transferLongOpposite(permissionGroupPo.getPermissionGroupId()));
                RRolePermission rRolePermission = new RRolePermission();
                rRolePermission.setRoleId(permissionGroupRoleIdPo.getRoleId()).setPermissionId(permissionGroup.getPermissionId());
                rRolePermissionDao.insert(rRolePermission);
            }else if (ObjectUtils.isEmpty(permissionGroupPo.getPermissionGroupId()) && permissionGroupPo.getPermissionIds().size() == 1){
                // 如果是 点击授权单资源 并且 父权限组就这一个资源的 条件
                Permission permission = permissionDao.selectParentPermissionBySubPermissionId(permissionGroupPo.getPermissionIds().get(0));
                RRolePermission rRolePermission = new RRolePermission();
                rRolePermission.setRoleId(permissionGroupRoleIdPo.getRoleId()).setPermissionId(permission.getId());
                rRolePermissionDao.insert(rRolePermission);
            }
            permissionGroupPo.getPermissionIds().forEach(permissionId -> {
                RRolePermission rRolePermission = new RRolePermission();
                rRolePermission.setRoleId(permissionGroupRoleIdPo.getRoleId()).setPermissionId(permissionId);
                rRolePermissionDao.insert(rRolePermission);
            });
        });
        return R.createSuccess(REnum.RSuccessResponse.UPDATE_SUCCESS_RESPONSE);
    }
}
